<?php

/**
 * Store
 *
 */
class Storemanagement_Model_Store extends Zend_Db_Table_Abstract
{
	public $_name = "store";
    public function __construct($config = array())
    {
        $this->_db = Zend_Db_Table::getDefaultAdapter();
        parent::__construct($config);
    }
	
	public function count($parameter) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$where = "(1=1)";
		
		$sql = "select count(*) as COUNT from " . $this->_name
				. " U WHERE $where";
		$r = $db->query($sql)->fetch();
		return $r['COUNT'];
	}
	
	public function SelectAll($parameter, $limit, $offset) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$where = "(1=1)";
		$sql = "SELECT 
					*
				FROM " . $this->_name. " U 
				WHERE $where limit $offset,$limit
	                ";
		$r = $db->query($sql)->fetchAll();
		return $r;
	}
	
	public function SelectAllNoPaging() {
		$db = Zend_Db_Table::getDefaultAdapter();
		
		$sql = "SELECT 
					*
				FROM " . $this->_name;
		$r = $db->query($sql)->fetchAll();
		return $r;
	}
	
	public function AddNewStore($params) {
		$id = $params['storeid'];
		$db = Zend_Db_Table::getDefaultAdapter();
		try {
			if($id ==0){
							$data = array(
											'NAME'=> $params['NAME'],
											'ADDRESS'=> $params['ADDRESS'],
											'EMAIL'=> $params['ADDRESS'],
											'CREATEDDATE'=> $params['CREATEDDATE'],
											'LASTUPDATE'=> $params['LASTUPDATE'],
											'STATUS'=> $params['STATUS'],
											'STORECATEGORYID'=> $params['STORECATEGORYID'],
											'PHONE'=> $params['PHONE'],
											'SITEURL'=> $params['SITEURL'],
											'FACEBOOKURL'=> $params['FACEBOOKURL'],
											'LOWESTPRICE'=> $params['LOWESTPRICE'],
											'HIGHESTPRICE'=> $params['HIGHESTPRICE'],
											'WARD'=> $params['WARD'],
											'DISTRICT'=> $params['DISTRICT'],
											'CITY'=> $params['CITY'],
											'DESCRIPTION'=> $params['DESCRIPTION'],
											'WORKINGTIME'=> $params['WORKINGTIME'],
											'USERID'=> $params['USERID'],
											'LASTDEAL'=> $params['LASTDEAL'],
											'TOTALDEAL'=> $params['TOTALDEAL'],
											'PAGEVIEW'=> $params['PAGEVIEW'],
											'ISFEATURED'=> $params['ISFEATURED']	
						);
						$db->insert($this->_name, $data);
			}
			else{
					$data = array(							
									'STOREID'=> $params['STOREID'],
									'NAME'=> $params['NAME'],
									'ADDRESS'=> $params['ADDRESS'],
									'EMAIL'=> $params['ADDRESS'],
									'CREATEDDATE'=> $params['CREATEDDATE'],
									'LASTUPDATE'=> $params['LASTUPDATE'],
									'STATUS'=> $params['STATUS'],
									'STORECATEGORYID'=> $params['STORECATEGORYID'],
									'PHONE'=> $params['PHONE'],
									'SITEURL'=> $params['SITEURL'],
									'FACEBOOKURL'=> $params['FACEBOOKURL'],
									'LOWESTPRICE'=> $params['LOWESTPRICE'],
									'HIGHESTPRICE'=> $params['HIGHESTPRICE'],
									'WARD'=> $params['WARD'],
									'DISTRICT'=> $params['DISTRICT'],
									'CITY'=> $params['CITY'],
									'DESCRIPTION'=> $params['DESCRIPTION'],
									'WORKINGTIME'=> $params['WORKINGTIME'],
									'USERID'=> $params['USERID'],
									'LASTDEAL'=> $params['LASTDEAL'],
									'TOTALDEAL'=> $params['TOTALDEAL'],
									'PAGEVIEW'=> $params['PAGEVIEW'],
									'ISFEATURED'=> $params['ISFEATURED']							
								  );
				$db->update($this->_name, $data,"STOREID = ".$id);
			}
		}catch (Zend_Exception $e) {
			echo ($e->getMessage());
			exit;
		}
	}
	
	public function DeleteStore($id){
		$db = Zend_Db_Table::getDefaultAdapter();
		$where = $db->quoteInto('STOREID = ?', $id);
		$db->delete($this->_name, $where);
	}
	
	public function FindById($id) {
		$result = $this->getDefaultAdapter()
		->query("SELECT * FROM $this->_name WHERE STOREID = ?", $id);
		return $result->fetch();
	}
	
	public function CheckExitsID($id) {
		$db = Zend_Db_Table::getDefaultAdapter();
		$sql = "select * from $this->_name where STOREID='" . $id . "'";
		$stmt = $db->query($sql);
		$r = $stmt->fetch();
		if ($r)
		return 1;
		else
		return 0;
	}

	public function CheckExistsName($name) {
		if($name == '') {
			return 0;
		}
		else 
		{
			$db = Zend_Db_Table::getDefaultAdapter();
			$sql = "select * from $this->_name where NAME='" . $name . "'";
			$stmt = $db->query($sql);
			$r = $stmt->fetch();
			if ($r)
			return 1;
			else
			return 0;
		}
	}
}